package com.aepryus.loom;

import java.util.*;
import java.text.*;
import com.aepryus.util.*;

public abstract class FromString {
	private static DateFormat dateFormat1 = new SimpleDateFormat("M/d/yy k:mm");
	private static DateFormat dateFormat2 = new SimpleDateFormat("M/d/yy");
	private static DateFormat dateFormat3 = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
	private static DateFormat dateFormat4 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
	private static DateFormat dateFormat5 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	public static boolean toBoolean (String string) {
		try {
			return "true".equals(string);
		} catch (Exception e) {
		    return false;
		}
	}
	public static int toInt (String string) {
		try {
		    return Integer.parseInt(string);
		} catch (Exception e) {
		    return 0;
		}
	}
	public static long toLong (String string) {
		try {
			return Long.parseLong(string);
		} catch (Exception e) {
		    return 0;
		}
	}
	public static double toDouble (String string) {
		try {
		    return Double.parseDouble(string);
		} catch (Exception e) {
		    return 0;
		}
	}
	public static String toString (String string) {
		return string;
	}
	public static Date toDate (String string) {
		try {
			return dateFormat5.parse(string);
		} catch (Exception e) {
		    try {
				return dateFormat2.parse(string);
		    } catch (Exception e2) {
			    try {
					return dateFormat3.parse(string);
			    } catch (Exception e3) {
				    try {
						return dateFormat4.parse(string);
				    } catch (Exception e4) {
//					    throw new RuntimeException("Date Parse Exception|"+string);
					    return null;
	    		    }
				}
		    }
		}
	}
	public static DateTime toDateTime (String string) {
		try {
			return new DateTime(dateFormat1.parse(string));
		} catch (Exception e) {
		    try {
				return new DateTime(dateFormat3.parse(string));
		    } catch (Exception e2) {
				return null;
			}
		}
	}
	public static Time toTime (String string) {
		return new Time(string);
	}

	public static void initialize (Controller controller) {
		controller.addStringTo(new StringTo () {
			public String getName () {return "boolean";}
			public Object fromString (String string) {return FromString.toBoolean(string);}
		});
		controller.addStringTo(new StringTo () {
			public String getName () {return "int";}
			public Object fromString (String string) {return FromString.toInt(string);}
		});
		controller.addStringTo(new StringTo () {
			public String getName () {return "long";}
			public Object fromString (String string) {return FromString.toLong(string);}
		});
		controller.addStringTo(new StringTo () {
			public String getName () {return "double";}
			public Object fromString (String string) {return FromString.toDouble(string);}
		});
		controller.addStringTo(new StringTo () {
			public String getName () {return "java.lang.String";}
			public Object fromString (String string) {return string;}
		});
		controller.addStringTo(new StringTo () {
			public String getName () {return "java.util.Date";}
			public Object fromString (String string) {return FromString.toDate(string);}
		});
		controller.addStringTo(new StringTo () {
			public String getName () {return "com.aepryus.util.DateTime";}
			public Object fromString (String string) {return FromString.toDateTime(string);}
		});
		controller.addStringTo(new StringTo () {
			public String getName () {return "com.aepryus.util.Time";}
			public Object fromString (String string) {return FromString.toTime(string);}
		});
	}
}

